package com.ksd.web.controller.system;

import java.io.File;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ksd.common.annotation.Log;
import com.ksd.common.config.PlayControlConfig;
import com.ksd.common.core.controller.BaseController;
import com.ksd.common.core.domain.AjaxResult;
import com.ksd.common.core.page.TableDataInfo;
import com.ksd.common.enums.BusinessType;
import com.ksd.common.utils.poi.ExcelUtil;
import com.ksd.system.domain.SysBoard;
import com.ksd.system.service.ISysBoardService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ksd.system.domain.SysApkVersion;
import com.ksd.system.service.ISysApkVersionService;

/**
 * 播放器版本列表Controller
 * 
 * @author y.wu
 * @date 2025-04-12
 */
@RestController
@RequestMapping("/system/apkVersion")
public class SysApkVersionController extends BaseController
{
    @Autowired
    private ISysApkVersionService sysApkVersionService;
    @Autowired
    private ISysBoardService sysBoardService;

    /**
     * 查询播放器版本列表列表
     */
    @PreAuthorize("@ss.hasPermi('system:apkVersion:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysApkVersion sysApkVersion)
    {
        startPage();
        List<SysApkVersion> list = sysApkVersionService.selectSysApkVersionList(sysApkVersion);
        SysBoard sysBoard = sysBoardService.selectSysBoardById(SysBoard.serviceFileId);
        String serviceIpUrl = sysBoard.getUrl();
        for (SysApkVersion apkVersion : list) {
            apkVersion.setUrl(serviceIpUrl + apkVersion.getUrl());
        }
        return getDataTable(list);
    }

    /**
     * 导出播放器版本列表列表
     */
    @PreAuthorize("@ss.hasPermi('system:apkVersion:export')")
    @Log(title = "播放器版本列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysApkVersion sysApkVersion)
    {
        List<SysApkVersion> list = sysApkVersionService.selectSysApkVersionList(sysApkVersion);
        ExcelUtil<SysApkVersion> util = new ExcelUtil<SysApkVersion>(SysApkVersion.class);
        util.exportExcel(response, list, "播放器版本列表数据");
    }

    /**
     * 获取播放器版本列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:apkVersion:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysApkVersionService.selectSysApkVersionById(id));
    }

    /**
     * 新增播放器版本列表
     */
    @PreAuthorize("@ss.hasPermi('system:apkVersion:add')")
    @Log(title = "播放器版本列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysApkVersion sysApkVersion)
    {
        return toAjax(sysApkVersionService.insertSysApkVersion(sysApkVersion));
    }

    /**
     * 修改播放器版本列表
     */
    @PreAuthorize("@ss.hasPermi('system:apkVersion:edit')")
    @Log(title = "播放器版本列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysApkVersion sysApkVersion)
    {
        return toAjax(sysApkVersionService.updateSysApkVersion(sysApkVersion));
    }

    /**
     * 删除播放器版本列表
     */
    @PreAuthorize("@ss.hasPermi('system:apkVersion:remove')")
    @Log(title = "播放器版本列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for (Long id : ids) {

            SysBoard sysBoard = sysBoardService.selectSysBoardById(SysBoard.serviceFileId);
            SysApkVersion sysApkVersion = sysApkVersionService.selectSysApkVersionById(id);
            String url = sysApkVersion.getUrl();
            url = url.substring(sysBoard.getUrl().length());
            url = url.substring(PlayControlConfig.getMappingPath().length());
            url = PlayControlConfig.getNginxRootPath()+url;

            logger.info("待删除文件:"+url);
            File file = new File(url);
            File parentFile = file.getParentFile();
            if (parentFile.exists()) {
                boolean delete = parentFile.delete();
                logger.info("文件删除结果:"+delete+"==========="+parentFile.getAbsolutePath());
            }
        }
        return toAjax(sysApkVersionService.deleteSysApkVersionByIds(ids));
    }

    public static void main(String[] args) {
        String url = "http://192.168.10.254:8080/storage/video/playcontrol_jar_version/20250904094600/playcontrol-admin.jar";
        String serverUrl = "http://192.168.10.254:8080";
        String mapperUrl = "/storage/video";
        String nginxUrl = "/ksd/www/storage/video";
        url = url.substring(serverUrl.length());
        url = url.substring(mapperUrl.length());
        url = nginxUrl + url;
        System.out.println(url);
    }
}
